package com.demo.userprofile.component.utils;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.roaringbitmap.longlong.Roaring64NavigableMap;

/**
 * BitMap代码示例
 *
 * @author userprofile_demo
 */
public class BitMapUtil {

    // 将BitMap写入到指定文件中
    public static void downloadFile(Roaring64NavigableMap bitMap) {
        try {
            String filePath = "/filePath/test.txt";
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, false)));
            bitMap.forEach(rawId -> {
                String entityId = String.valueOf(rawId);
                try {
                    out.write(entityId);
                    out.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        // 新建bitmap，并添加元素
        Roaring64NavigableMap bitMap = new Roaring64NavigableMap();
        bitMap.add(1L);
        bitMap.add(2L);

        // BitMap时间的交并差操作
        Roaring64NavigableMap bitMapA = new Roaring64NavigableMap();
        Roaring64NavigableMap bitMapB = new Roaring64NavigableMap();
        bitMapA.and(bitMapB);
        bitMapA.or(bitMapB);
        bitMapA.andNot(bitMapB);

        // BitMap元素遍历
        bitMap.forEach(item -> {
            // 对元素item的相关操作
        });

        downloadFile(bitMap);
    }
}
